package com.auth.manage.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.auth.manage.common.user.api.request.UserModifyRequest;
import com.auth.manage.user.domain.entity.User;
import com.auth.manage.user.domain.entity.convertor.UserConvertor;
import com.auth.manage.user.domain.service.UserService;
import com.auth.manage.user.infra.exception.UserErrorCode;
import com.auth.manage.user.infra.exception.UserException;
import com.auth.manage.web.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import param.UserModifyPwdParam;

@RequestMapping("user")
@RestController
@RequiredArgsConstructor
@Slf4j
public class UserController {
    private final UserService userService;

    @GetMapping("getUserInfoByUserId")
    public Result getUserInfoByUserId(){
        long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        Assert.notNull(user,()->new UserException(UserErrorCode.USER_NOT_FOUND));
        return Result.success(UserConvertor.INSTANCE.do2VO(user));
    }

    // 这个接口的qps会小于根据id查询，所以相比之下可以不建缓存
    @GetMapping("getUserInfoByUserId")
    public Result getUserInfoByTelephone(String telephone){
        User user = userService.getUserByTelephone(telephone);
        Assert.notNull(user,()->new UserException(UserErrorCode.USER_NOT_FOUND));
        return Result.success(UserConvertor.INSTANCE.do2VO(user));
    }

    // 注销
    @GetMapping("logoutAccount")
    public Result logoutAccount(){
        long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);
        Assert.notNull(user,()->new UserException(UserErrorCode.USER_NOT_FOUND));
        // todo 用户状态改为注销还是逻辑删除，还是二者一起
        return Result.success();
    }

    // todo 能否多个Controller接口共享一个修改用户信息的Service,然后RPC也共享
    // todo 能否把修改用户各种信息，如密码、昵称、头像都只做一个Controller接口
    // todo 修改昵称需不需要输入密码，怎么实现每月只能修改三次
    @PostMapping("/modifyNickName")
    public Result modifyNickName(String nickName){
        // todo hollis把校验用户是否存在的逻辑都放在了Controller
        long userId = StpUtil.getLoginIdAsLong();
        UserModifyRequest request = new UserModifyRequest();
        request.setNickName(nickName);
        request.setUserId(userId);
        userService.modifyUserInfo(request);
        return Result.success();
    }

    @PostMapping("/modifyPassword")
    public Result modifyPassword(@Validated @RequestBody UserModifyPwdParam userModifyRequest){
        // todo 校验密码是否正确的逻辑能否写在Controller
        long userId = StpUtil.getLoginIdAsLong();
        UserModifyRequest request = new UserModifyRequest();
        request.setUserId(userId);
        request.setPassword(userModifyRequest.getNewPassword());
        request.setPassword(userModifyRequest.getOldPassword());
        userService.modifyUserInfo(request);
        return Result.success();
    }

    // todo 实名认证接口
    // todo 修改用户头像接口
}

